﻿using System;
using System.Drawing;
using System.Web.UI.WebControls;
using System.Drawing.Imaging;
using DGP.Framework.Utils.Drawing;
using System.Web;
using System.Net;
using System.IO;
using System.Text;

public partial class CustomControls_ImageEditor : System.Web.UI.UserControl
{
    private Bitmap bitmap;
    private string imageUrl;

    protected void Page_Load( object sender, EventArgs e )
    {
        imageUrl = HttpContext.Current.Request.ApplicationPath + "Image.aspx?ImageID=" + this.ID;
    }

    protected void btnLoad_Click( object sender, EventArgs e )
    {
        bitmap = DGP.Framework.Utils.Drawing.Image.GetImageFromURL( txtURL.Text );
        Redraw();
    }

    protected void btnLoadWithOverlay_Click( object sender, EventArgs e )
    {
        bitmap = DGP.Framework.Utils.Drawing.Image.GetImageFromURL( txtURL.Text );
        RedrawWithOverlay();
    }

    private void Redraw()
    {
        Session[this.ID] = bitmap;
        imgShow.ImageUrl = imageUrl;
        imgShow.Width = bitmap.Width;
        imgShow.Height = bitmap.Height;
        imgShow.Visible = false;
        imgShow.Visible = true;
    }
    private void RedrawWithOverlay()
    {
        Bitmap overlayBitmap = DGP.Framework.Utils.Drawing.Image.GetImageFromURL( "http://about-children.liveweb.ro/overlayImage.gif" );
        ImageMimeTypes.ImageCornerEnum corner = ImageMimeTypes.ImageCornerEnum.BottomRight;
        bitmap = ImageActions.OverlayBitmap( bitmap, overlayBitmap, 50, corner );
        Session[this.ID] = bitmap;
        imgShow.ImageUrl = imageUrl;
        imgShow.Width = bitmap.Width;
        imgShow.Height = bitmap.Height;
        imgShow.Visible = false;
        imgShow.Visible = true;
    }

    protected void btnbtnSaveToDisk_Click( object sender, EventArgs e )
    {
        Bitmap image = Session[this.ID] as Bitmap;
        image.Save( Constants.PHYSICAL_IMAGES_PATH + "test.jpg" );
    }

    #region working solution 1
    /*private Bitmap bitmap;
    private string imageUrl;

    protected void Page_Load( object sender, EventArgs e )
    {
        imageUrl = HttpContext.Current.Request.ApplicationPath + "Image.aspx";
        if( !Page.IsPostBack )
        {
            imgShow.BorderWidth = new Unit( 10 );
            imgShow.Width = 100;
            imgShow.Height = 200;
            imgShow.Visible = true;
            ddlOverlayPosition.Items.AddRange( new ListItem[] { 
                     new ListItem( "TopLeft" ), 
                     new ListItem( "TopRight" ), 
                     new ListItem( "BottomRight" ), 
                     new ListItem( "BottomLeft" ), 
                     new ListItem( "Center" ) } );
        }
    }

    protected void btnLoad_Click( object sender, EventArgs e )
    {
        bitmap = DGP.Framework.Utils.Drawing.Image.GetImageFromURL( txtURL.Text );
        Redraw();
    }

    protected void btnRotate90_Click( object sender, EventArgs e )
    {
        bitmap = DGP.Framework.Utils.Drawing.Image.GetImageFromURL( imageUrl );
        bitmap = ImageActions.RotateBitmapRight90( bitmap );
        Redraw();
    }

    private void Redraw()
    {
        Session["image"] = bitmap;
        imgShow.ImageUrl = imageUrl;
        imgShow.Width = bitmap.Width;
        imgShow.Height = bitmap.Height;
    }

    protected void btnRotate180_Click( object sender, EventArgs e )
    {
        bitmap = DGP.Framework.Utils.Drawing.Image.GetImageFromURL( imageUrl );
        bitmap = ImageActions.RotateBitmapRight180( bitmap );
        Redraw();
    }

    protected void btnRotate270_Click( object sender, EventArgs e )
    {
        bitmap = DGP.Framework.Utils.Drawing.Image.GetImageFromURL( imageUrl );
        bitmap = ImageActions.RotateBitmapRight270( bitmap );
        Redraw();
    }

    protected void btnOverlay_Click( object sender, EventArgs e )
    {
        Bitmap overlayBitmap = DGP.Framework.Utils.Drawing.Image.GetImageFromURL( this.txtOverlay.Text );
        bitmap = Session["image"] as Bitmap;

        if( overlayBitmap != null )
        {
            ImageMimeTypes.ImageCornerEnum corner = ImageMimeTypes.ImageCornerEnum.BottomRight;

            corner = (ImageMimeTypes.ImageCornerEnum)
                Enum.Parse( corner.GetType(), this.ddlOverlayPosition.SelectedValue );

            bitmap = ImageActions.OverlayBitmap( bitmap, overlayBitmap, Int32.Parse( this.txtAlpha.Text ), corner );
        }
        Redraw();
    }

    protected void btnbtnSaveToDisk_Click( object sender, EventArgs e )
    {
        Bitmap image = Session["modifiedBitmap"] as Bitmap;
        image.Save( Constants.PHYSICAL_IMAGES_PATH + "test.jpg" );
    }
    */

    #endregion
}
